Skip to content

Conversation

@MaksymMalicki
Copy link
Contributor

@MaksymMalicki MaksymMalicki commented Nov 28, 2025

This PR addresses #3282 (comment), and uses more descriptive felt.Hash type for trie node hash fields and felt.StateRootHash for all the state root hash fields in all triedb and trie2 packages

@codecov
Copy link

codecov bot commented Nov 28, 2025

Codecov Report

❌ Patch coverage is 68.49315% with 46 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.18%. Comparing base (584e500) to head (ba21c5d).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
core/trie2/trie.go 28.57% 20 Missing ⚠️
core/felt/hash.go 33.33% 8 Missing ⚠️
core/trie2/trieutils/id.go 75.00% 6 Missing ⚠️
core/trie2/triedb/pathdb/journal.go 37.50% 5 Missing ⚠️
core/trie2/databasetest.go 70.00% 3 Missing ⚠️
core/trie2/trieutils/accessors.go 50.00% 2 Missing ⚠️
core/trie2/triedb/hashdb/database.go 94.11% 1 Missing ⚠️
core/trie2/triedb/rawdb/database.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3316      +/-   ##
==========================================
- Coverage   76.20%   76.18%   -0.02%     
==========================================
  Files         346      346              
  Lines       32690    32773      +83     
==========================================
+ Hits        24912    24969      +57     
- Misses       5987     6011      +24     
- Partials     1791     1793       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MaksymMalicki MaksymMalicki force-pushed the maksym/refactor-triedb-address branch 2 times, most recently from 9210f6f to 090e07a Compare December 3, 2025 17:59
@MaksymMalicki MaksymMalicki force-pushed the maksym/refactor-triedb-hash branch from ad10776 to 6bee396 Compare December 3, 2025 19:19
Base automatically changed from maksym/refactor-triedb-address to main December 4, 2025 05:22
@MaksymMalicki MaksymMalicki force-pushed the maksym/refactor-triedb-hash branch from 6a421db to 886e905 Compare December 5, 2025 11:49
Comment on lines 152 to 154
classNodes map[felt.Hash]map[trieutils.Path]trienode.TrieNode
contractNodes map[felt.Hash]map[trieutils.Path]trienode.TrieNode
contractStorageNodes map[felt.Hash]map[felt.Address]map[trieutils.Path]trienode.TrieNode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

felt.Hash is the most general type, but I think it is being miss-used.

felt.Hash means that there is not a good type to define it and it makes sense to be a general type, for example the return type of hashing function.

On the other hand, here we know the semantic meaning each hash has here, so we should use the appropriate types.

As far as I understand, it should be:

  1. classNodes keys should be of type felt.ClassHash
  2. contractNodes should be of type felt.Address since it is a contract address
  3. contractStorageNodes should be of type felt.Address to felt.StorageAddress (or felt.StorageSlot). <- The latter types are to be defined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think its correct or I'm missing something here. In this particular example, The felt.Hash in the hashmap mimics the layertree, which maps state root -> trie nodeset. So the felt.Hash represents the state root. The trieutils.Path represents the class hash for class trie, address for contract trie and the storage slot for the contract storage tries

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, then I think we should have a StateRootHash

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added


// Child to parent layer relationship
childToParent map[felt.Felt]felt.Felt
childToParent map[felt.Hash]felt.Hash
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar coment to the previous ones, what would be the right hash here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a mapping of child state root to the parent state root. Since its calculated from a few components using PoseidonHash (result of hashing function), I'd say it's correct or we introduce a special felt-like type for the state commitment

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here we could use StateRootHash again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me add it

Copy link
Contributor

@EgeCaner EgeCaner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

func ReadStateID(r db.KeyValueReader, root *felt.Felt) (uint64, error) {
key := db.StateIDKey(root)
func ReadStateID(r db.KeyValueReader, root *felt.StateRootHash) (uint64, error) {
key := db.StateIDKey((*felt.Felt)(root))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make StateIDKey receive felt.StateRootHash?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If too big a change for this PR then @MaksymMalicki please leave a todo to update it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, good catch, added

func TestCacheMisses(t *testing.T) {
cache := newCleanCache(1024 * 1024)
owner := felt.FromUint64[felt.Address](1234567890)
owner := *felt.NewFromUint64[felt.Address](1234567890)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must have slipped during rebase, changed


// Different owner
diffOwner := felt.FromUint64[felt.Address](9876543210)
diffOwner := *felt.NewFromUint64[felt.Address](9876543210)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, changed


var (
testOwner = felt.FromUint64[felt.Address](1234567890)
testOwner = *felt.NewFromUint64[felt.Address](1234567890)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

contractRootNode, err := trienode.DecodeNode(contractRootBlob, contractRootHash, 0, contractClassTrieHeight)
contractRootNode, err := trienode.DecodeNode(
contractRootBlob,
(*felt.Felt)(contractRootHash),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A todo for DecodeNode saying that the contractRootHash should be updated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

require.NoError(t, err)

owner := id.Owner()
nodeHash := node.Hash()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A todo for node.Hash() to make it's return type a felt.Hash

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

_, found := database.dirtyCache.getNode(
&owner,
path,
(*felt.Hash)(&nodeHash),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same idea here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Comment on lines +329 to +330
classRoot felt.Hash
contractRoot felt.Hash
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of hashes are these?

Copy link
Contributor Author

@MaksymMalicki MaksymMalicki Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are dependent on the hashing function used by given trie, but those hashes are the hashed values of the children, so we either add something like felt.TrieNodeHash or we stick to the felt.Hash (which IMO is descriptive enough since the implementation of the trie is general purpose)

Comment on lines 524 to 526
err := database.GetTrieRootNodes(
(*felt.Hash)(&classRootHash),
(*felt.Hash)(&contractRootHash),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refactor or add a todo for this function to change the parameters to the appropiate types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

owner *felt.Address,
path *Path,
hash *felt.Felt,
hash *felt.Hash,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this Hash something more specific than felt.Hash

Copy link
Contributor Author

@MaksymMalicki MaksymMalicki Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, it is the poseidon/pedersen hash of the children of this node. I think maybe some type like felt.TrieNodeHash would be okey, but generally imo felt.Hash is descriptive enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants